package com.eascs.account.service;

import com.eascs.admin.common.service.BaseSevice;
import com.eascs.admin.o2o.loanAccount.entity.O2oLoanapplyAccountEntity;
import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.inject.Inject;

@Service
public class AccountService extends BaseSevice {
	private final Logger logger = LoggerFactory.getLogger(AccountService.class);

	@Inject
	protected JdbcSession o2oJdbcSession;

	public void initLoanAccount() {
		StringBuffer querySql = new StringBuffer("  SELECT l.loanapply_no FROM o2o_loanapply l WHERE l.bill_code is not null AND " +
				" l.loanapply_no  NOT IN ( SELECT loanapply_no FROM o2o_loanapply_account ) ");
		logger.info("======================初始化贷款单账户开始=======================");
		int index = 0;

		while (index >= 0) {
			SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, 100, index);
			pageQuery.setQuerySql(querySql.toString());

			PageResult<Object[]> pageResult = o2oJdbcSession.findDataWithPage(pageQuery);
			if (pageResult.getReturnDataSize() == 0) return;

			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					O2oLoanapplyAccountEntity entity = new O2oLoanapplyAccountEntity();
					entity.setLoanapplyNo((String) objs[0]);
					entity.setStatus("0");
					entity.setJqstatus("0");
					o2oJdbcSession.insert(entity);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("初始化贷款单账户异常", e);
				}
			}

			index++;
			if (pageResult.getPageTotal() <= index) {
				index = -1;
			}
		}

		logger.info("======================初始化贷款单账户结束=======================");
	}
}
